package com.base.common.cfg;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

/**
 * @author Davin qsd
 * 
 */
public class PropertiesHelper {
	Properties p;

	public PropertiesHelper(Properties p) {
		this.p = p;
	}

	public Properties getProperties() {
		return p;
	}

	public String getProperty(String key, String defaultValue) {
		return getProperties().getProperty(key, defaultValue);
	}

	public String getProperty(String key) {
		return getProperties().getProperty(key);
	}

	public String getRequiredProperty(String key) {
		String value = getProperty(key);
		if (value == null || "".equals(value.trim())) {
			throw new IllegalStateException("required property is blank by key=" + key);
		}
		return value;
	}

	public Integer getInt(String key) {
		if (getProperty(key) == null) {
			return null;
		}
		return Integer.parseInt(getRequiredProperty(key));
	}

	public int getInt(String key, int defaultValue) {
		if (getProperty(key) == null) {
			return defaultValue;
		}
		return Integer.parseInt(getRequiredProperty(key));
	}

	public int getRequiredInt(String key) {
		return Integer.parseInt(getRequiredProperty(key));
	}

	public long getRequiredLong(String key) {
		return Long.parseLong(getRequiredProperty(key));
	}

	public Boolean getBoolean(String key) {
		if (getProperty(key) == null) {
			return null;
		}
		return Boolean.parseBoolean(getRequiredProperty(key));
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		if (getProperty(key) == null) {
			return defaultValue;
		}
		return Boolean.parseBoolean(getRequiredProperty(key));
	}

	public boolean getRequiredBoolean(String key) {
		return Boolean.parseBoolean(getRequiredProperty(key));
	}

	public String getNullIfBlank(String key) {
		String value = getProperties().getProperty(key);
		if (value == null || "".equals(value.trim())) {
			return null;
		}
		return value;
	}

	public PropertiesHelper setProperty(String key, String value) {
		p.setProperty(key, value);
		return this;
	}

	/**
	 * String [] 0：key 1：value
	 * 
	 * @param pre
	 * @return
	 */
	public List<String[]> getPropertysPre(String pre) {
		List<String[]> ret = new ArrayList<String[]>();
		Set<Object> keylist = p.keySet();
		for (Object obj : keylist) {
			String key = (String) obj;
			if (key.startsWith(pre)) {
				String tmp[] = { key, p.getProperty(key) };
				ret.add(tmp);
			}
		}
		return ret;
	}

	public void clear() {
		p.clear();
	}

	public Set<Entry<Object, Object>> entrySet() {
		return p.entrySet();
	}

	public Enumeration<?> propertyNames() {
		return p.propertyNames();
	}

	/**
	 * 初始化Configuration类
	 * 
	 * @param filePath
	 *            要读取的配置文件的路径+名称
	 */
	public static Properties loadByFilePath(String filePath) {
		Properties propertie = new Properties();
		try {
			FileInputStream inputFile = new FileInputStream(filePath);
			propertie.load(inputFile);
			inputFile.close();
		} catch (FileNotFoundException ex) {
			System.out.println("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
			ex.printStackTrace();
			return null;
		} catch (IOException ex) {
			System.out.println("装载文件--->失败!");
			ex.printStackTrace();
			return null;
		}
		return propertie;
	}

	/**
	 * 通过二进制文件生成配置
	 * 
	 * @param byt
	 * @return
	 */
	public static Properties loadByByte(byte[] byt) {
		Properties propertie = new Properties();
		ByteArrayInputStream inputFile = null;
		try {
			inputFile = new ByteArrayInputStream(byt);
			propertie.load(inputFile);

		} catch (FileNotFoundException ex) {
			System.out.println("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
			ex.printStackTrace();
			return null;
		} catch (IOException ex) {
			System.out.println("装载文件--->失败!");
			ex.printStackTrace();
			return null;
		} finally {
			if (inputFile != null) {
				try {
					inputFile.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return propertie;
	}

}
